package com.linfeng.component.common.response;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.linfeng.component.common.exception.CustomException;
import com.linfeng.component.common.exception.CustomExceptionCode;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonSerialize(using = ResponseEntitySerializer.class)
public class ResponseEntity<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    public ResponseEntity() {
    }

    public ResponseEntity(CustomException exception) {
        this(exception.getCode(), exception.getMessage());
    }

    public ResponseEntity(CustomExceptionCode code, T data) {
        this(code.getCode(), code.getDescription(), data);
    }

    public ResponseEntity(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseEntity(Integer code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public ResponseEntity(T data) {
        this(CustomExceptionCode.OK, data);
    }

    public static ResponseEntity body(Object data) {
        return new ResponseEntity<>(data);
    }
}
